/** 
 * File-Name:ProjectRepository.java
 *
 * Created on 2011-6-2 下午09:35:02
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package xiaoyou.service;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;

import xiaoyou.dao.ProjectDao;
import xiaoyou.entity.Project;
import xiaoyou.neoutil.CommonService;
import xiaoyou.neoutil.DaoSupport;
import xiaoyou.neoutil.Page;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午09:35:02
 */
public class ProjectRepository extends CommonService {
	private static Logger log = Logger.getLogger(ProjectRepository.class);
	private String SQL_INSERT = "INSERT INTO project(uuid, name, introduction, description, contactInfo, authorId, publishDate, updateDate, entityStatus, reserve01, reserve02, reserve03, reserve04, reserve05) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
	private String SQL_UPDATE = "UPDATE project SET uuid=?, name=?, introduction=?, description=?, contactInfo=?, authorId=?, updateDate=?, entityStatus=?, reserve01=?, reserve02=?, reserve03=?, reserve04=?, reserve05=? WHERE id=?";
	private String SQL_DELETE_BY_ID = "UPDATE project SET entityStatus=0 WHERE id=?";
	private String SQL_GET_BY_ID = "SELECT * FROM project WHERE id=? AND entityStatus=1";
	private String SQL_GET_BY_UUID = "SELECT * FROM project WHERE uuid=? AND entityStatus=1";
	private String SQL_GET_ALL = "SELECT * FROM project WHERE entityStatus=1 ORDER BY publishDate DESC";
	private String SQL_GET_LATEST_ALL = "SELECT * FROM project WHERE entityStatus=1 ORDER BY updateDate DESC LIMIT 0,?";

	public int save(Project project) {
		Connection connection = DaoSupport.getConnection();
		ProjectDao projectDao = new ProjectDao(connection);

		try {
			Object[] params = { project.getUuid(), project.getName(),
					project.getIntroduction(), project.getDescription(),
					project.getContactInfo(), project.getAuthorId(),
					new Date(), new Date(), 1, project.getReserve01(),
					project.getReserve02(), project.getReserve03(),
					project.getReserve04(), project.getReserve05() };
			projectDao.execute(SQL_INSERT, params);
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存Project对象出现异常！！");
			e.printStackTrace();
			return FAIL;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public int update(Project project) {
		Connection connection = DaoSupport.getConnection();
		ProjectDao projectDao = new ProjectDao(connection);

		try {
			Object[] params = { project.getUuid(), project.getName(),
					project.getIntroduction(), project.getDescription(),
					project.getContactInfo(), project.getAuthorId(),
					new Date(), project.getEntityStatus(),
					project.getReserve01(), project.getReserve02(),
					project.getReserve03(), project.getReserve04(),
					project.getReserve05(), project.getId() };
			projectDao.execute(SQL_UPDATE, params);
			return SUCCESS;
		} catch (Exception e) {
			log.error("更新Project对象出现异常！！");
			e.printStackTrace();
			return FAIL;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public int delete(long id) {
		Connection connection = DaoSupport.getConnection();
		ProjectDao projectDao = new ProjectDao(connection);

		try {
			Object[] params = { id };
			projectDao.execute(SQL_DELETE_BY_ID, params);
			return SUCCESS;
		} catch (Exception e) {
			log.error("假删除Project对象出现异常！！");
			e.printStackTrace();
			return FAIL;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public Project get(long id) {
		Connection connection = DaoSupport.getConnection();
		ProjectDao projectDao = new ProjectDao(connection);

		try {
			Object[] params = { id };
			List<Project> projects = projectDao.find(SQL_GET_BY_ID, params);
			if (projects == null) {
				return null;
			} else if (projects.size() > 1) {
				log.error("根据ID查询到的Project个数不唯一，为防止错误，返回null!");
				return null;
			} else {
				return projects.get(0);
			}
		} catch (Exception e) {
			log.error("根据ID取得Project对象出现异常！！");
			e.printStackTrace();
			return null;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public Project get(String uuid) {
		Connection connection = DaoSupport.getConnection();
		ProjectDao projectDao = new ProjectDao(connection);

		try {
			Object[] params = { uuid };
			List<Project> projects = projectDao.find(SQL_GET_BY_UUID, params);
			if (projects == null) {
				return null;
			} else if (projects.size() > 1) {
				log.error("根据UUID查询到的Project个数不唯一，为防止错误，返回null!");
				return null;
			} else {
				Project project = projects.get(0);

				UserRepository userRepository = new UserRepository();
				project.setAuthor(userRepository.get(project.getAuthorId()));

				return project;
			}
		} catch (Exception e) {
			log.error("根据UUID取得Project对象出现异常！！");
			e.printStackTrace();
			return null;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public Page<Project> getProjects(final Page<Project> page) {
		Connection connection = DaoSupport.getConnection();
		ProjectDao projectDao = new ProjectDao(connection);

		try {
			Page<Project> projects = projectDao.find(page, SQL_GET_ALL);
			return projects;
		} catch (Exception e) {
			log.error("取得所有Project对象出现异常！！");
			e.printStackTrace();
			return null;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public List<Project> getLatestProjects(int topNum) {
		Connection connection = DaoSupport.getConnection();
		ProjectDao projectDao = new ProjectDao(connection);

		try {
			Object[] params = { topNum };
			List<Project> projects = projectDao
					.find(SQL_GET_LATEST_ALL, params);
			return projects;
		} catch (Exception e) {
			log.error("取得所有Fund对象出现异常！！");
			e.printStackTrace();
			return null;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		Project project = new Project();
		project.setName("Name2");
		project.setAuthorId(1L);
		// project.setId(1L);

		ProjectRepository projectRepository = new ProjectRepository();
		projectRepository.save(project);
		// projectRepository.update(project);

		List<Project> projects = projectRepository.getLatestProjects(3);
		System.out.println(projects.size());
	}
}
